﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LibraryDS.CDBNames;
using LibraryDS;
using LibraryUS;
using IPCommon;

namespace LibraryMain
{
    public partial class f011_V_DM_DOC_GIA_DE : Form
    {
        public f011_V_DM_DOC_GIA_DE()
        {
            InitializeComponent();
            format_controls();
        }

        #region Public Interface
        public void display()
        {
            m_dat_han_su_dung.Enabled = true;
            this.ShowDialog();
        }
        public void display_for_update(US_V_DM_DOC_GIA ip_us_v_dm_doc_gia)
        {
            m_us_v_dm_doc_gia = ip_us_v_dm_doc_gia;
            m_e_form_mode = DataEntryFormMode.UpdateDataState;
            this.ShowDialog();
        }
        public void display_for_insert()
        {
            m_e_form_mode = DataEntryFormMode.InsertDataState;
            this.ShowDialog();
        }

        #endregion

        #region Members
        DataEntryFormMode m_e_form_mode;
        US_V_DM_DOC_GIA m_us_v_dm_doc_gia = new US_V_DM_DOC_GIA();
        US_DM_DOC_GIA m_us_dm_doc_gia = new US_DM_DOC_GIA();
        #endregion

        #region Data Structure
        
        #endregion

        #region Private Method

        private void enable_combobox(bool ip_b)
        {
            m_cbo_doc_gia_nguoi_lon.Enabled = ip_b;
            m_cbo_ma_doc_gia_nguoi_lon.Enabled = ip_b;
        }

        private void enable_control_by_m_ckb_la_nguoi_lon_check()
        {
            if (m_ckb_la_doc_gia_nguoi_lon.Checked == true)
            {
                enable_combobox(false);
            }
            else
            {
                enable_combobox(true);
            }
        }
        private void load_data_to_combobox()
        {
            DS_V_DM_DOC_GIA v_ds_v_dm_doc_gia = new DS_V_DM_DOC_GIA();
            m_us_v_dm_doc_gia.FillDataset(v_ds_v_dm_doc_gia,"WHERE ID_DOC_GIA_NGUOI_LON IS NULL");
            //load data to cbo m_cbo_doc_gia_nguoi_lon
            m_cbo_doc_gia_nguoi_lon.DataSource = v_ds_v_dm_doc_gia.V_DM_DOC_GIA;
            m_cbo_doc_gia_nguoi_lon.DisplayMember = V_DM_DOC_GIA.TEN_DOC_GIA_NGUOI_LON;
            m_cbo_doc_gia_nguoi_lon.ValueMember = V_DM_DOC_GIA.ID_DOC_GIA_NGUOI_LON;
            //load data to cbo m_cbo_ma_doc_gia_nguoi_lon
            m_cbo_ma_doc_gia_nguoi_lon.DataSource = v_ds_v_dm_doc_gia.V_DM_DOC_GIA;
            m_cbo_ma_doc_gia_nguoi_lon.DisplayMember = V_DM_DOC_GIA.MA_DOC_GIA_NGUOI_LON;
            m_cbo_ma_doc_gia_nguoi_lon.ValueMember = V_DM_DOC_GIA.ID_DOC_GIA_NGUOI_LON;

        }

        private void format_controls()
        {
            CControlFormat.setFormStyle(this);
            set_define_events();
            enable_control_by_m_ckb_la_nguoi_lon_check();
        }
        private void us_object_to_form()
        {
            load_data_to_combobox();
            m_cbo_doc_gia_nguoi_lon.SelectedValue = m_us_v_dm_doc_gia.dcID_DOC_GIA_NGUOI_LON;
            m_cbo_ma_doc_gia_nguoi_lon.SelectedValue = m_us_v_dm_doc_gia.dcID_DOC_GIA_NGUOI_LON;

            m_txt_ho.Text = m_us_v_dm_doc_gia.strHO;
            m_txt_ten.Text = m_us_v_dm_doc_gia.strTEN;
            m_txt_ten_dem.Text = m_us_v_dm_doc_gia.strTEN_DEM;
            m_txt_ma_doc_gia.Text = m_us_v_dm_doc_gia.strMA_DOC_GIA;
            m_dat_ngay_sinh.Value = m_us_v_dm_doc_gia.datNGAY_SINH;
            m_txt_so_nha.Text = m_us_v_dm_doc_gia.strSO_NHA;
            m_txt_duong.Text = m_us_v_dm_doc_gia.strDUONG;
            m_txt_quan.Text = m_us_v_dm_doc_gia.strQUAN;
            m_txt_dien_thoai.Text = m_us_v_dm_doc_gia.strDIEN_THOAI;
            m_dat_han_su_dung.Value = m_us_v_dm_doc_gia.datHAN_SU_DUNG;
            if (m_us_v_dm_doc_gia.dcID_DOC_GIA_NGUOI_LON != m_us_v_dm_doc_gia.dcID)
            {
                m_ckb_la_doc_gia_nguoi_lon.Checked = true;
            }
            else m_ckb_la_doc_gia_nguoi_lon.Checked = false;
           
        }
        private void form_to_us_object()
        {
            m_us_dm_doc_gia.dcID = m_us_v_dm_doc_gia.dcID;
            m_us_dm_doc_gia.strMA_DOC_GIA = m_txt_ma_doc_gia.Text;
            m_us_dm_doc_gia.strHO = m_txt_ho.Text;
            m_us_dm_doc_gia.strTEN = m_txt_ten.Text;
            m_us_dm_doc_gia.strTEN_DEM = m_txt_ten_dem.Text;
            m_us_dm_doc_gia.datNGAY_SINH = m_dat_ngay_sinh.Value;
            m_us_dm_doc_gia.strSO_NHA = m_txt_so_nha.Text;
            m_us_dm_doc_gia.strDUONG = m_txt_duong.Text;
            m_us_dm_doc_gia.strQUAN = m_txt_quan.Text;
            m_us_dm_doc_gia.datHAN_SU_DUNG = m_dat_han_su_dung.Value;
            if (m_ckb_la_doc_gia_nguoi_lon.Checked == true)
            {
                m_us_dm_doc_gia.SetID_DOC_GIA_NGUOI_LONNull();
            }
            else
            {
                m_us_dm_doc_gia.dcID_DOC_GIA_NGUOI_LON = CIPConvert.ToDecimal(m_cbo_doc_gia_nguoi_lon.SelectedValue);
            }
            
        }
        private void set_inital_form_load()
        {
            switch (m_e_form_mode)
            {
                case DataEntryFormMode.UpdateDataState:
                    enable_combobox(false);
                    us_object_to_form();
                    break;
                case DataEntryFormMode.InsertDataState:
                    load_data_to_combobox();
                    enable_combobox(true);
                    break;
                default:
                    break;
            }
        }
        private void set_define_events()
        {
            this.Load +=new EventHandler(f011_V_DM_DOC_GIA_DE_Load);
            m_cmd_update.Click += new EventHandler(m_cmd_update_Click);
            m_cmd_exit.Click += new EventHandler(m_cmd_exit_Click);
            this.m_ckb_la_doc_gia_nguoi_lon.CheckedChanged += new System.EventHandler(this.m_ckb_la_doc_gia_nguoi_lon_CheckedChanged);
        }
        private bool check_validate_data_is_ok()
        {
            if (!CValidateTextBox.IsValid(m_txt_ho, DataType.StringType, allowNull.NO, true))
            {
                return false;
            }
            if (!CValidateTextBox.IsValid(m_txt_ten, DataType.StringType, allowNull.NO, true))
            {
                return false;
            }
            if (!CValidateTextBox.IsValid(m_txt_ma_doc_gia, DataType.StringType, allowNull.NO, true))
            {
                return false;
            }
            if (DateTime.Now.Year - m_dat_ngay_sinh.Value.Year >= 18)
            {
                if (m_ckb_la_doc_gia_nguoi_lon.Checked == false)
                {
                    BaseMessages.MsgBox_Confirm("Với Ngày Sinh ở trên, độc giả phải Là Độc Giả Người Lớn!");
                    m_ckb_la_doc_gia_nguoi_lon.Checked = true;
                }
            }
            if (DateTime.Now.Year - m_dat_ngay_sinh.Value.Year < 18)
            {
                if (m_ckb_la_doc_gia_nguoi_lon.Checked == true)
                {
                    BaseMessages.MsgBox_Confirm("Với Ngày Sinh ở trên, độc giả không thể Là Độc Giả Người Lớn!");
                    m_ckb_la_doc_gia_nguoi_lon.Checked = false;
                }
            }
            return true;
        }
        private void save_data()
        {
            if (check_validate_data_is_ok() == false) return;
            form_to_us_object();
            switch (m_e_form_mode)
            {
                case DataEntryFormMode.UpdateDataState:
                    m_us_dm_doc_gia.Update();
                    break;
                case DataEntryFormMode.InsertDataState:
                    m_us_dm_doc_gia.Insert();
                    break;
                default:
                    break;
            }
            BaseMessages.MsgBox_Infor("Dữ liệu đã được cập nhât!");
            this.Close();
        }

        #endregion

        #region Event
        private void f011_V_DM_DOC_GIA_DE_Load(object sender, EventArgs e)
        {
            try
            {
                set_inital_form_load();
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(v_e);
            }
        }
        private void m_cmd_update_Click(object sender, EventArgs e)
        {
            try
            {
                save_data();
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(v_e);
            }
        }
        private void m_cmd_exit_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(v_e);
            }
        }
        private void m_ckb_la_doc_gia_nguoi_lon_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                enable_control_by_m_ckb_la_nguoi_lon_check();
                if (DateTime.Now.Year - m_dat_ngay_sinh.Value.Year >= 18)
                {
                    if (m_ckb_la_doc_gia_nguoi_lon.Checked == false)
                    {
                        BaseMessages.MsgBox_Confirm("Với Ngày Sinh ở trên, độc giả phải Là Độc Giả Người Lớn!");
                        m_ckb_la_doc_gia_nguoi_lon.Checked = true;
                        enable_control_by_m_ckb_la_nguoi_lon_check();
                    }
                }
                if (DateTime.Now.Year - m_dat_ngay_sinh.Value.Year < 18)
                {
                    if (m_ckb_la_doc_gia_nguoi_lon.Checked == true)
                    {
                        BaseMessages.MsgBox_Confirm("Với Ngày Sinh ở trên, độc giả không thể Là Độc Giả Người Lớn!");
                        m_ckb_la_doc_gia_nguoi_lon.Checked = false ;
                        enable_control_by_m_ckb_la_nguoi_lon_check();
                    }
                }
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(v_e);
            }
        }
        #endregion

       
    }
}
